package jp.jig.hackathon;

import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

public class AlarmNetworkServer {
	public static void main(String[] args) {
		new AlarmNetworkServer();
	}
	
	public AlarmNetworkServer() {
		
		//System.out.println(this.getClass().getClassLoader().getResource("www").toExternalForm());
		
		Server server = new Server(8080);
		/*
		Server server = new Server();
		SelectChannelConnector connector = new SelectChannelConnector();
		connector.setPort(8080);
		server.addConnector(connector);
		*/
		
		ResourceHandler rh = new ResourceHandler();
		rh.setResourceBase(this.getClass().getClassLoader().getResource("www").toExternalForm());
		ContextHandler ctx = new ContextHandler();
		ctx.setContextPath("/alarm");
		ctx.setHandler(rh);
		
		ServletContextHandler svCtx = new ServletContextHandler(ServletContextHandler.SESSIONS);
		svCtx.setContextPath("/alarm");
		svCtx.addServlet(new ServletHolder(new Register()), "/registration");
		svCtx.addServlet(new ServletHolder(new GCMPush()), "/gcm");
		
		ContextHandlerCollection ctxhdlCol = new ContextHandlerCollection();
		ctxhdlCol.setHandlers(new Handler[] {ctx, svCtx});
		server.setHandler(ctxhdlCol);
		
		try {
			server.start();
			server.join();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
